package com.crm.controller;

import com.crm.common.annotation.Log;
import com.crm.common.enums.BusinessType;
import com.crm.common.result.AjaxResult;
import com.crm.common.result.PageResult;
import com.crm.common.result.Result;
import com.crm.domain.Contract;
import com.crm.dto.ContractDto;
import com.crm.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/contract")
public class ContractController {

    @Autowired
    private ContractService contractService;

    // 新增合同
    @PostMapping
    @Log(title = "合同管理", businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody Contract contract) {
        contractService.saveContract(contract);
        return AjaxResult.success();
    }

    //根据id查询合同
    @GetMapping("/{id}")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    public AjaxResult getById(@PathVariable Long id) {
        log.info("根据id查询合同：{}",id);
        Contract contract = contractService.getContractById(id);
        return AjaxResult.success(contract);
    }
    //根据id修改回显
    @GetMapping("/detail/{id}")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    public AjaxResult getContractById(@PathVariable Long id) {
        log.info("根据id修改回显：{}",id);
        Contract contract = contractService.getContractById(id);
        return AjaxResult.success(contract);
    }

    //分页查询合同
    @GetMapping("/list")
    @Log(title = "合同管理", businessType = BusinessType.EXPORT)
    public PageResult page(ContractDto dto) {
        log.info("分页查询合同：{}",dto);
        PageResult pageResult = contractService.pageList(dto);
        return pageResult;
    }



}
